/*
 * @Descripttion:
 * @version: 1.0.0
 * @Author: YinJianFeng
 * @Date: 2024-03-29 16:24:47
 */
import { CMEVENTS, BaseCommand } from '@amcax/base';
import { Picker3D, PickerMgr } from '@amcax/renderer';
import { wsApi } from '../../../../server/wsApi';
import { userEvents } from '../../../../module';

let isDeleting = false;
let Picker = PickerMgr.getPicker<Picker3D>('3d');
export const deleteModels = async () => {
  if (isDeleting) return;
  const model_dxid = Array.from(Picker.getSelectedModels().keys());
  if (model_dxid.length) {
    isDeleting = true;
    wsApi.delete(model_dxid);
  }
};

const isDerleted = () => {
  isDeleting = false;
};

export const cmdDelete: BaseCommand = {
  id: 'CmdDelete',
  icon: 'CmdDelete',
  label: 'command.delete',
  noHighlight: true,

  invoke: deleteModels,

  initialDisabled: true,
  subscribeDisabled: (setDisabled: (disabled: boolean) => void) => {
    const listener = (can: boolean) => {
      setDisabled(!can);
    };
    userEvents
      .on(CMEVENTS.CAN_DELETE, listener)
      .on(CMEVENTS.MODEL_REMOVED, isDerleted);
    return () => userEvents.off(CMEVENTS.CAN_DELETE, listener);
  },
};
